import oss2
from django.conf import settings


class OSSClient:
    def __init__(self):
        self.auth = oss2.Auth(settings.ALIYUN_OSS_ACCESS_KEY_ID, settings.ALIYUN_OSS_ACCESS_KEY_SECRET)
        self.bucket = oss2.Bucket(self.auth, settings.ALIYUN_OSS_ENDPOINT, settings.ALIYUN_OSS_BUCKET_NAME)

    def upload_file(self, file, filename):
        """上传文件到阿里云OSS"""
        try:
            self.bucket.put_object(filename, file)
            return f"https://{settings.ALIYUN_OSS_BUCKET_NAME}.{settings.ALIYUN_OSS_ENDPOINT}/{filename}"
        except oss2.exceptions.OssError as e:
            print(f"上传文件失败: {e}")
            return None
